import util from '../../common/js/util'
import NProgress from 'nprogress'
import axios from 'axios';
import address from "../../../models/dict/address.js"  //任务字典模块
import moment from 'moment'

import task from "m/dict/task.js"  //任务字典模块
import array from "../../../models/comm/array.js"  //数组操作模块
import yz from "../../../models/comm/yz.js"  //数组操作模块
import Vue from 'vue'

import myTaskInfo from './myTaskInfo.vue'
import py from '../../../models/comm/webPy';
import { bus } from '../../../models/comm/bus.js';

const dFlow = require('../../../models/dict/flow');

import customerOrderShow from '../Order/customerOrderShow';



export default {
    data() {
        return {
            taskList: [],
            taskClassArray: task.classArray(),
            currObj: task.fmtTask(),
            page: 1,
            filters: {},
            listLoading: false,
            total: 1,
            taskId: "",

            o: {

                showTaskDialog: false
            },
            showAcceptTask: false,
            currTaskId: "",
            expEndTime: new Date(),

            pickerOptions1: {
                shortcuts: [{
                    text: '今天',
                    onClick(picker) {
                        picker.$emit('pick', new Date());
                    }
                }, {
                    text: '明天',
                    onClick(picker) {
                        const date = new Date();
                        date.setTime(date.getTime() + 3600 * 1000 * 24);
                        picker.$emit('pick', date);
                    }
                }, {
                    text: '后天',
                    onClick(picker) {
                        const date = new Date();
                        date.setTime(date.getTime() + 3600 * 1000 * 24 * 2);
                        picker.$emit('pick', date);
                    }
                },
                {
                    text: '大后天',
                    onClick(picker) {
                        const date = new Date();
                        date.setTime(date.getTime() + 3600 * 1000 * 24 * 3);
                        picker.$emit('pick', date);
                    }
                }
                ]
            },

        }
    },
    methods: {
        getCurrTr: function (task) {

            if (task._id == this.taskId) {
                return { currTr: true }
            }
            else {
                return { currTr: false }
            }

        },
        test() {

            this.$bus.emit('test', {});
        },
        getTaskList: function () {


            axios.post("/a", {
                class: "task",
                para: "getMyTaskList",
                j: {
                    data: {

                        companyCode: localStorage.companyCode

                    },
                    page: this.page,
                    sort: { statusId: 1 },
                    needCustomerOrder: true


                }

            }).then((res) => {

                res.data.list.forEach(function (element) {
                    element.show = false
                }, this);
                this.taskList = res.data.list;

                this.total = res.data.total
                this.listLoading = false;
                NProgress.done();


            });
        },

        showTask(_id) {

            this.taskId = _id
            this.o.showTaskDialog = true
        },
        fmtDate: function (time) {

            if (!time) {
                return "-"
            }

            if (time == "") {
                return "-"
            }
            return moment(time).format("YYYY-MM-DD<br />HH:mm:ss");

        },
        handleCurrentChange: function (page) {
            this.page = page
            this.getTaskList();
        },
        saveTask: function (params) {
            this.currObj.companyCode = localStorage.companyCode;
            axios.post("/a", {
                class: "task",
                para: "saveTask",
                j: this.currObj

            }).then((res) => {
                if (res.data.re == "ok") {
                    this.ShowSave = false;
                    this.getTaskList();
                } else {
                    alert(JSON.stringify(res.data));
                }
            });
        },
        querySearchMember: function (query, cb) {
            var data = {
                "$or": [
                    { name: { $regex: query } },
                    { namePy: { $regex: py.firstLetter(query) } }
                ]
            }
            //  console.log(JSON.stringify(data));
            if (query !== '') {
                this.loading = true;
                axios.post("/a", {
                    class: "member",
                    para: "getMemberList",
                    j: {
                        data: data

                    }

                }).then((res) => {

                    this.loading = false;

                    var list = res.data.list;

                    list.forEach(function (member) {
                        member.value = member.name;

                    }, this);
                    cb(list)

                });

            }
        },
        getModelNo(t) {
            return task.getModelNo(t)

        },
        selManager: function (params) {


            var b = false;
            this.currObj.managers.forEach(function (manager) {
                if (params._id == manager._id) {

                    b = true;
                    return false;
                }

            }, this);
            if (!b) {
                this.currObj.managers.push({

                    name: params.name,
                    _id: params._id
                });

            }

            //     console.log(JSON.stringify(this.currObj.managers));

        },
        selMember: function (params) {


            var b = false;
            this.currObj.members.forEach(function (member) {
                if (params._id == member._id) {

                    b = true;
                    return false;
                }

            }, this);
            if (!b) {
                this.currObj.members.push({

                    name: params.name,
                    _id: params._id
                });

            }

            //     console.log(JSON.stringify(this.currObj.managers));

        }

        ,
        handleClose: function (tag, tags) {

            tags.splice(tags.indexOf(tag), 1);
        },
        beforeEnter: function () {
            console.log('beforeEnter');
        }
        ,
        getFlowClassName(classId) {
            return dFlow.getClassName(classId)
        },

        getTaskClassName(row) {
            var r = ""

            this.taskClassArray.forEach(function (element) {
                if (row.taskClassId == element.id) {

                    //  
                    r = element.name;


                    return false;
                }

            }, this);
            return r;
        },
        taskClassChange() {
            console.log('改变了任务类别');
            this.currObj = task.fmtTask(this.currObj);

        },
        getTaskStatus(statusId) {


            return yz.IsNullThen(task.getStatus(statusId), {});

        }
        ,


        toAcceptTask(_id) {

            this.showAcceptTask = true

            this.currTaskId = _id
            this.expEndTime = new Date();
        },
        //接受任务 
        acceptTask() {
            this.$confirm('是否确定完成日期的预计?', '确认提交?', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {

                axios.post("/a", {
                    class: "task",
                    para: "acceptTask",
                    j: {
                        _id: this.currTaskId,
                        expEndTime: this.expEndTime

                    }

                }).then((res) => {
                    if (!res.data.err) {
                        this.showAcceptTask = false
                        this.getTaskList();
                        this.$message({
                            type: 'success',
                            message: '任务已接受!'
                        });
                        this.taskId = this.currTaskId
                    }
                    else {
                        this.$message({
                            showClose: true,
                            message: res.data.err,
                            type: 'error'

                        });
                    }
                });

            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });


        },
        doneTask(_id) {

            this.$confirm('提示:提交后任务列表将会刷新', '确认完成?', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {

                axios.post("/a", {
                    class: "task",
                    para: "doneTask",
                    j: {
                        _id: _id,
                        expEndTime: this.expEndTime

                    }

                }).then((res) => {
                    if (!res.data.err) {
                        this.showAcceptTask = false
                        this.getTaskList();
                        this.$message({
                            type: 'success',
                            message: '任务已接受!'
                        });
                        this.taskId = _id
                    }
                    else {
                        this.$message({
                            showClose: true,
                            message: res.data.err,
                            type: 'error'

                        });
                    }
                });

            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });



        }



    },


    mounted() {
        this.getTaskList();
    },
    components: {

        myTaskInfo,
        customerOrderShow
    }


} 